package com.ken.blog.controller.admin;

import com.ken.blog.domain.Article;
import com.ken.blog.domain.Category;
import com.ken.blog.domain.ArticleRepository;
import com.ken.blog.domain.CategoryRepository;
import com.ken.blog.tools.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin/article")
public class AdminArticleController {
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @RequestMapping("")
    public ModelAndView index(String page) {
        ModelAndView mv = new ModelAndView("admin/article/index");

        //处理分页请求
        int pageSize = Pagination.getPageSize(page);
        Pageable pageable = new PageRequest(pageSize, 10);
        Page<Article> pageList = articleRepository.findAll(pageable);

        List<Article> articleList = pageList.getContent();
        //分页功能
        Map pagination = Pagination.simgle(pageList.getTotalPages(), pageSize + 1);

        mv.addObject("pagination", pagination);
        mv.addObject("list", articleList);

        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("admin/article/add");

        if (request.getMethod().equals("POST")) {
            Category category = this.categoryRepository.findOne(Long.parseLong(request.getParameter("cid")));

            Article article = new Article();
            article.setTitle(request.getParameter("title"));
            article.setBody(request.getParameter("body"));
            article.setCategory(category);

            Article result = this.articleRepository.save(article);
            System.out.println(result);

            mv.addObject("message", "添加成功");
            mv.addObject("url", "/admin/article");
            mv.setViewName("admin/message");
        }

        List<Category> list = this.categoryRepository.findAll();

        mv.addObject("list", list);

        return mv;
    }

    @RequestMapping("/edit/{id}")
    public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) {
        ModelAndView mv = new ModelAndView("admin/article/edit");

        Article article = this.articleRepository.findOne(id);
        if (article == null) {
            mv.addObject("message", "不存在的分类");
            mv.addObject("url", "/admin/article");
            mv.setViewName("admin/message");
            return mv;
        }

        if (request.getMethod().equals("POST")) {
            String title = request.getParameter("title");
            String body = request.getParameter("body");
            Long cid = Long.parseLong(request.getParameter("cid"));
            Category category = this.categoryRepository.findOne(cid);
            int result = this.articleRepository.update(title, body, category, id);

            System.out.println(result);

            mv.addObject("message", "编辑成功");
            mv.addObject("url", "/admin/article");
            mv.setViewName("admin/message");
            return mv;
        }

        List<Category> list = this.categoryRepository.findAll();

        mv.addObject("list", list);

        mv.addObject("article", article);

        return mv;
    }

    @RequestMapping("/delete/{id}")
    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) {
        ModelAndView mv = new ModelAndView("admin/message");

        Article article = this.articleRepository.findOne(id);
        if (article == null) {
            mv.addObject("message", "不存在的文章");
            mv.addObject("url", "/admin/article");
            return mv;
        }

        this.articleRepository.delete(id);

        mv.addObject("message", "删除成功");
        mv.addObject("url", "/admin/article");
        return mv;
    }
}
